import 'dart:convert';
import 'dart:io';

import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_module_sale/config/env/env_sale.dart';
import 'package:flutter_device_info/flutter_device_info.dart';

import 'config/cache_info_key.dart';
import 'config/native_config.dart';

void main() {
  FlutterDeviceInfo()
      .getCacheInfo(CacheInfoKey.STORE_CONFIG, CacheInfoKey.KEY_CONFIG)
      .then((String nativeConfigContent) {
    NativeConfig nativeConfig;
    try {
      nativeConfig = NativeConfig.fromJson(json.decode(nativeConfigContent));
    } catch (e) {
      nativeConfig = NativeConfig();
    }
    EnvType type;
    NetWorkConfig netWorkConfig = NetWorkConfig(
        localProxyIPAddress: nativeConfig?.httpProxyHost,
        localProxyPort: nativeConfig?.httpProxyPort?.toString(),
        connectTimeout: 15000,
        receiveTimeout: 15000,
        appId: nativeConfig?.appId ?? "",
        clientSecret: nativeConfig?.clientSecret ?? "");
    switch (nativeConfig?.nativeEnv) {
      case "TEST":
        type = EnvType.COMMIT_TEST;
        break;
      case "PRE":
        type = EnvType.PRE_RELEASE;
        break;
      case "RELEASE":
        type = EnvType.RELEASE;
        netWorkConfig.appId = Platform.isAndroid ? "8" : "7";
        netWorkConfig.clientSecret = Platform.isAndroid
            ? "GMQw0LiPju9MoaQ9MIex2aEtoe4l9UsILdxofYKq"
            : "7PyAeAIlp8umpzXYKesjxSZZf4mts0TkrOoiuafe";
        break;
      case "DEBUG":
        type = EnvType.DEBUG;
        break;
      default:
        type = EnvType.RELEASE;
        netWorkConfig.appId = Platform.isAndroid ? "8" : "7";
        netWorkConfig.clientSecret = Platform.isAndroid
            ? "GMQw0LiPju9MoaQ9MIex2aEtoe4l9UsILdxofYKq"
            : "7PyAeAIlp8umpzXYKesjxSZZf4mts0TkrOoiuafe";
        break;
    }
    SaleEnv(type:type, netWorkConfig:netWorkConfig, nativeConfig:nativeConfig);
  });
}
